package com.cloud.wms.ui.returned.inventory;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.eventBus.Event;
import com.cloud.core.eventBus.EventBusUtil;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.in.contract.InShelfContract;
import com.cloud.wms.ui.in.entity.InCommodityEntity;
import com.cloud.wms.ui.in.entity.InOrderEntity;
import com.cloud.wms.ui.in.entity.ShelfBatchEntity;
import com.cloud.wms.ui.in.presnter.InShelfPresenter;
import com.cloud.wms.ui.returned.contact.InventoryContract;
import com.cloud.wms.ui.returned.entity.InventoryInfoEntity;
import com.cloud.wms.ui.returned.presenter.InventoryPresenter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-01-26 15:28
 * @Description:
 */
public class SelectBatchActivity extends BaseBarListActivity<InventoryInfoEntity.CommodityListBean.BatchInfoBean, InventoryPresenter> implements InventoryContract.View {
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    ArrayList<InventoryInfoEntity.CommodityListBean.BatchInfoBean> batchInfoBeans;

    @Override
    protected String barTitle() {
        return "选择批次";
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_in_shelf_select_batch;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        btnSubmit.setEnabled(false);
        batchInfoBeans = getIntent().getParcelableArrayListExtra(IntentKey.LIST_KEY);
        mAdapter.setNewInstance(batchInfoBeans);
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, InventoryInfoEntity.CommodityListBean.BatchInfoBean item) {
        helper.setText(R.id.tv_produce_date, item.getProductionDate().length() > 11 ? item.getProductionDate().substring(0, 11) : item.getProductionDate())
                .setText(R.id.tv_deadline_date, item.getExpirationDate().length() > 11 ? item.getExpirationDate().substring(0, 11) : item.getExpirationDate())
                .setText(R.id.tv_in_date, item.getInDate().length() > 11 ? item.getInDate().substring(0, 11) : item.getInDate())
                .setTextColorRes(R.id.tv_produce_date, item.isSelect() ? R.color.font_white : R.color.font_black)
                .setTextColorRes(R.id.tv_produce_date_name, item.isSelect() ? R.color.font_white : R.color.font_gray)
                .setTextColorRes(R.id.tv_deadline_date, item.isSelect() ? R.color.font_white : R.color.font_black)
                .setTextColorRes(R.id.tv_deadline_date_name, item.isSelect() ? R.color.font_white : R.color.font_gray)
                .setTextColorRes(R.id.tv_in_date, item.isSelect() ? R.color.font_white : R.color.font_black)
                .setTextColorRes(R.id.tv_in_date_name, item.isSelect() ? R.color.font_white : R.color.font_gray)
                .setBackgroundResource(R.id.ll_item_content, item.isSelect() ? R.drawable.bg_item_common_select : R.drawable.bg_item_common);
    }

    @Override
    protected int getResId() {
        return R.layout.activity_returned_inventory_select_batch;
    }

    @Override
    protected void itemClickListener(BaseQuickAdapter<InventoryInfoEntity.CommodityListBean.BatchInfoBean, BaseViewHolder> mAdapter, View view, int position) {
        super.itemClickListener(mAdapter, view, position);
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            InventoryInfoEntity.CommodityListBean.BatchInfoBean entity = mAdapter.getData().get(i);
            if (i == position) {
                entity.setSelect(!entity.isSelect());
            } else {
                entity.setSelect(false);
            }
        }
        mAdapter.notifyDataSetChanged();
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            InventoryInfoEntity.CommodityListBean.BatchInfoBean entity = mAdapter.getData().get(i);
            if (entity.isSelect()) {
                btnSubmit.setEnabled(true);
                return;
            }
        }
        btnSubmit.setEnabled(false);
    }

    @Override
    protected void setupComponent() {
    }

    @Override
    public void onSuccess(Message message) {
    }

    @OnClick(R.id.btn_submit)
    public void onViewClicked() {
        ArrayList<InventoryInfoEntity.CommodityListBean.BatchInfoBean> entities = new ArrayList<>();
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            InventoryInfoEntity.CommodityListBean.BatchInfoBean entity = mAdapter.getData().get(i);
            if (entity.isSelect()) {
                entities.add(entity);
            }
        }
        if (entities.isEmpty()) {
            info("请选择批次后提交");
            return;
        }
        EventBusUtil.sendEvent(new Event<>(EventConfig.SWITCH_BATCH, entities.get(0)));
        success("批次选择成功！");
        this.finish();
    }

    public static void start(Context context, ArrayList<InventoryInfoEntity.CommodityListBean.BatchInfoBean> batchInfoBeans) {
        Intent intent = new Intent(context, SelectBatchActivity.class);
        intent.putExtra(IntentKey.LIST_KEY, batchInfoBeans);
        context.startActivity(intent);
    }
}
